function checkUserInfo(pathName, fromPage, cb) {
    var that = this;

    wx.getSetting({
        success(res) {
            if (!res.authSetting['scope.userInfo']) {
                wx.showModal({
                    title: '温馨提示',
                    content: '为获得更好服务，建议开启授权!',
                    showCancel: false,
                    confirmText: '确定',
                    success: function(res) {
                        if (res.confirm) {
                            wx.navigateTo({
                                url: `/${pathName}?fromPage=${fromPage}&authSet=scope.userInfo`,
                            })
                        } else if (res.cancel) {
                            wx.navigateBack({
                                delta: 1, // 回退前 delta(默认为1) 页面
                                success: function(res) {
                                    // success
                                }
                            })
                        }
                    }
                })
            } else {
                wx.getUserInfo({
                    success: function(res) {
                        console.log('成功获取用户信息')
                        wx.setStorage({
                            key: 'userInfo',
                            data: res.userInfo,
                            success: function(res) {
                                console.log('userInfo写入缓存成功', res)
                            }
                        })
                    },
                    fail: function(res) {
                        console.log('wx.getUserInfo 接口调用失败，将无法获取用户信息', res)
                    }
                })
                typeof cb == "function" && cb(true)
            }
        },
        fail(err) {
            typeof cb == "function" && cb(false)
        }
    })
}

//检查用户录音授权 已经授权返回true 未授权会提示用户授权并返回授权结果
function checkRecord(pathName, fromPage, cb) {
    var that = this
    wx.getSetting({
        success: (res) => {
            console.log(res)
            if (!res.authSetting['scope.record']) {
                wx.authorize({
                    scope: 'scope.record',
                    success: (res) => {
                        typeof cb == "function" && cb(true)
                    },
                    fail: () => {
                        wx.showModal({
                            title: '温馨提示',
                            content: '需要你授权录音能力才能玩配音哦！',
                            showCancel: false,
                            confirmText: '确定',
                            success: function(res) {
                                if (res.confirm) {
                                    wx.navigateTo({
                                        url: `/${pathName}?fromPage=${fromPage}&authSet=scope.record`,
                                    })
                                } else if (res.cancel) {
                                    wx.navigateBack({
                                        delta: 1, // 回退前 delta(默认为1) 页面
                                        success: function(res) {
                                            // success
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            } else {
                typeof cb == "function" && cb(true)
            }
        },
        fail: function(res) {
            typeof cb == "function" && cb(false)
        }
    })
}

module.exports = {
    checkUserInfo,
    checkRecord
}